/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   TextConfig.cs
 * CreateData   :   2023/7/11 13:16:31
 * UnityVersion :   2021.3.20f1c1
 * Description  :   文本配置表
************************************************/

#if UNITY_EDITOR
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using UnityEditor;
#endif

using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;

namespace Main
{
    public class TextConfig : ScriptableObjectConfigBase<string>
    {
#if UNITY_EDITOR
        [Title("Body")]
        [SerializeField, Range(100, 1000), LabelText("最大遍历行数")] private int MaxRowConut = 200;
        [SerializeField, Range(100, 1000), LabelText("最大遍历列数")] private int MaxColConut = 200;
#endif
        
#if UNITY_EDITOR
        [Button("加载"), HorizontalGroup("Button")]
        private void LoadExcel()
        {
            Clear();
            //excel 2007之后版本
            XSSFWorkbook Mybook = new XSSFWorkbook(PathConfig.AbsolutePath + PathConfig.ExcelFilePath);
            //获取表名为  “TextConfig” 的表
            ISheet sheet = Mybook.GetSheet(nameof(TextConfig));
            // 遍历所有行
            for (int y = 0; y < MaxRowConut; y++)
            {
                //获取行
                IRow sheet_row = sheet.GetRow(y);
                //当前行为null时，跳过此行
                if (sheet_row == null) continue;
                //遍历行的所有列
                for (int x = 0; x < MaxColConut; x++)
                {
                    ICell cell = sheet_row.GetCell(x);
                    if (cell != null)
                    {
                        string text = cell.ToString();
                        string ID = ((x + 1) * 10000 + y + 1).ToString();
                        if (!string.IsNullOrWhiteSpace(text))
                        {
                            datas.Add(ID, text);
                        }
                    }
                }
            }
            Mybook.Close();
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
        }
        [Button("清空Null"), HorizontalGroup("Button")]
        private void ClearNull()
        {
            List<string> recoveryID = new List<string>();
            foreach (var item in datas)
            {
                if (string.IsNullOrWhiteSpace(item.Value))
                {
                    recoveryID.Add(item.Key);
                }
            }
            foreach (var item in recoveryID)
            {
                datas.Remove(item);
            }
        }
        [Button("清空"), HorizontalGroup("Button")]
        private void Clear()
        {
            datas.Clear();
        }
#endif
    }
}
